<template>
  <div>
    <MBX level1="数据统计" level2="数据报表"></MBX>
    <!-- 主体 -->
    <el-card class="mt16">
      <div ref="myChart" id="box"></div>
    </el-card>
  </div>
</template>

<script>
import * as echarts from "echarts";
export default {
  data() {
    return {
      myChart: null,
      preData: {
        title: {
          text: "用户来源",
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            label: {
              backgroundColor: "#E9EEF3",
            },
          },
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: [
          {
            boundaryGap: false,
          },
        ],
        yAxis: [
          {
            type: "value",
          },
        ],
      },
    };
  },
  mounted() {
    this.getChart();

    // 监听  窗口改变事件             一个方法
    window.addEventListener("resize", this.myChart.resize);
  },
  methods: {
    async getChart() {
      // 1. 获取节点
      const chartDom = this.$refs.myChart;
      //  2. 初始化
      this.myChart = echarts.init(chartDom);

      const res = await this.$API.tuBiaoApi();

      //3.配置图表数据option
      const option = { ...this.preData, ...res };

      //4.渲染生成图表
      this.myChart.setOption(option);
    },
  },
};
</script>

<style lang="scss" scoped>
#box {
  width: 100%;
  height: 600px;
}
</style>
